package com.anxin.service.impl;

import com.anxin.entity.DoctorSchedule;
import com.anxin.mapper.DoctorScheduleMapper;
import com.anxin.service.DoctorScheduleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.List;

@Service
public class DoctorScheduleServiceImpl extends ServiceImpl<DoctorScheduleMapper, DoctorSchedule> implements DoctorScheduleService {

    @Override
    public DoctorSchedule getScheduleByDoctorAndDate(String doctorId, LocalDate date) {
        return this.getOne(new LambdaQueryWrapper<DoctorSchedule>()
                .eq(DoctorSchedule::getDoctorId, doctorId)
                .eq(DoctorSchedule::getScheduleDate, date));
    }

    @Override
    public List<DoctorSchedule> getSchedulesByDepartmentAndDate(String departmentId, LocalDate date) {
        return this.list(new LambdaQueryWrapper<DoctorSchedule>()
                .eq(DoctorSchedule::getDepartmentId, departmentId)
                .eq(DoctorSchedule::getScheduleDate, date)
                .eq(DoctorSchedule::getStatus, 1));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScheduleStatus(String scheduleId, Integer status) {
        DoctorSchedule schedule = new DoctorSchedule();
        schedule.setId(scheduleId);
        schedule.setStatus(status);
        this.updateById(schedule);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateQuota(String scheduleId, Integer morningQuota, Integer afternoonQuota) {
        DoctorSchedule schedule = new DoctorSchedule();
        schedule.setId(scheduleId);
        schedule.setMorningQuota(morningQuota);
        schedule.setMorningRemaining(morningQuota);
        schedule.setAfternoonQuota(afternoonQuota);
        schedule.setAfternoonRemaining(afternoonQuota);
        this.updateById(schedule);
    }
} 